﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Contracts;

namespace Services
{
    public class ChannelManager
    {
        #region Fields

        /// <summary>
        /// 回调通道列表
        /// </summary>
       // private List<IPushCallback> callbackChannelList = new List<IPushCallback>();
        private Dictionary<string, IPushCallback> callbackChannelList = new Dictionary<string, IPushCallback>();

        /// <summary>
        /// 用于互斥锁的对象
        /// </summary>
        public static readonly object SyncObj = new object();

        #endregion Fields

        #region Single

        private static readonly Lazy<ChannelManager> instance = new Lazy<ChannelManager>(() => new ChannelManager());

        public static ChannelManager Instance
        {
            get { return instance.Value; }
        }

        protected ChannelManager()
        {
        }

        #endregion Single

        #region Methods

        /// <summary>
        /// 将回调通道加入到通道列表中进行管理
        /// </summary>
        /// <param name="callbackChannel"></param>
        public void Add(string SessionId, IPushCallback callbackChannel)
        {
            if (callbackChannelList.ContainsKey(SessionId))
            {
                Console.WriteLine("已存在重复通道");
            }
            else
            {
                lock (SyncObj)
                {
                    callbackChannelList.Add(SessionId, callbackChannel);
                    Console.WriteLine("添加了新的通道");
                }
            }
        }

        /// <summary>
        /// 从通道列表中移除对一个通道的管理
        /// </summary>
        /// <param name="callbackChannel"></param>
        public void Remove(string SessionId, IPushCallback callbackChannel)
        {
            if (!callbackChannelList.ContainsKey(SessionId))
            {
                Console.WriteLine("不存在待移除通道");
            }
            else
            {
                lock (SyncObj)
                {
                    callbackChannelList.Remove(SessionId);
                    Console.WriteLine("移除了一个通道");
                }
            }
        }

        /// <summary>
        /// 广播消息
        /// </summary>
        /// <param name="message"></param>
        public void NotifyMessage(string message)
        {
            if (callbackChannelList.Count > 0)
            {
                //避免对callbackChannelList的更改对广播造成的影响
                // IPushCallback[] callbackChannels = callbackChannelList.ToArray();
                foreach (var channel in callbackChannelList)
                {
                    //try
                    //{
                    //    string value = channel.ToString();
                    //    //广播消息
                    //    channel.NotifyMessage(message);

                    //}
                    //catch
                    //{
                    //    //对异常的通道进行处理
                    //    callbackChannelList.Remove(channel);
                    //}
                    channel.Value.NotifyMessage(message);
                }
            }
        }

        #endregion Methods
    }
}